package  
{
	import org.flixel.*;
	import org.flixel.plugin.photonstorm.*;
	
	/**
	 * ...
	 * @author le-ray_y
	 */
	public class Enemy extends FlxExtendedSprite 
	{
		//************************************************************************
		//** 							VARIABLES								**
		//************************************************************************
		
		// Enemy Graphics
		[Embed(source = '../../Assets/enemy.png')]
		private static var _enemyGraph:Class;
		
		// Animation reset handler
		private var _hasAnimReset:Boolean = false;
		
		// Level
		public var level:Number;
		
		// Damage
		public var damage:Number;
		
		// Speed
		public var speed:Number;
		
		// Healthbar
		public var _hpBar:FlxBar;
		
		//************************************************************************
		//** 							FUNCTIONS								**
		//************************************************************************
		
		public function Enemy(xPos:Number, yPos:Number, _level:Number, _speed:Number, onClick:Function) 
		{
			this.x = xPos;
			this.y = yPos;
			
			this.health = 10 * _level;
			this.damage = 1 * _level;
			
			this.level = _level;
			
			this.speed = _speed;
			this.maxVelocity.x = 75;
			
			// Graphics
			this.loadGraphic(_enemyGraph, true, false, 43, 37);
			
			// Animations & Play
			this.addAnimation("idle", [0]);
			this.addAnimation("walk", [0, 1], 5);
			this.addAnimation("death", [2, 3, 4, 5], 5);
			this.addAnimation("attack", [6, 7, 8], 5);
			this.addAnimation("attackRabbit", [6, 7, 8], 5);
			this.play("walk");
			
			// Health Bar
			_hpBar = new FlxBar(0, 0, FlxBar.FILL_LEFT_TO_RIGHT, 30, 5, this, "health", 0, this.health, true);
			_hpBar.trackParent( 0, -10);
			_hpBar.dirty = true;
			FlxG.state.add(_hpBar);
			
			this.enableMouseClicks(false);
			this.mousePressedCallback = onClick;
			
			// Create the enemy only when called in Game.as by makeEnemy(), so it doesn't exists before that
			this.exists = false;
		}
		
		override public function update():void
		{
			super.update();
			
			if (!this.alive)
			{
				this.play("death");
				if (_curFrame == 3 && _curAnim.name == "death")
				{
					kill();
					_hpBar.exists = false;
				}
			}
			
			if (_curAnim.name == "walk")
			{
				this.velocity.x += this.speed * FlxG.elapsed;
			}
			
			if (_curAnim.name == "attack")
			{
				if (_curFrame == 0)
				{
					_hasAnimReset = true;
				}
				if (_curFrame == 2 && _hasAnimReset)
				{
					Global.tower.damageTower(damage);
					_hasAnimReset = false;
				}
				
			}
			
			if (this.health <= 0)
			{
				this.alive = false;
			}
		}
		
		// Resets enemy for the recycling algo =p
		public function resetEnemy(X:Number,Y:Number, _level:Number, _speed:Number):void
		{
			this.x = X;
			this.y = Y;
			
			// Revive
			this.revive();
			
			// Anim
			this.play("walk");
			
			// Health Damage Level Speed
			this.health = 10 * _level;
			this.damage = 1 * _level;
			this.level = _level;
			this.speed = _speed;
			
			// HP Bar
			_hpBar.exists = true;
			_hpBar.setRange(0, this.health);
		}
		
	}

}